package com.cysyz.mylib.queryandcondsannotationparser.impl;

import java.lang.reflect.Field;

import com.cysyz.mylib.dao.Conds;
import com.cysyz.mylib.queryandcondsannotationparser.QueryAndCondsAnnotationParser;
import com.cysyz.mylib.queryandcondsannotationparser.QueryPrefixPostfixAndConds;
import com.cysyz.mylib.queryandcondsannotationparser.annotation.QueryCond;
import com.cysyz.mylib.queryandcondsannotationparser.annotation.QueryPrefixAndPostfix;

public class QueryAndCondsAnnotationParserImpl implements QueryAndCondsAnnotationParser {
	
	/** 
	 * @Title: generateQuery
	 * @Author: 崔勇
	 * @Date 2014-4-24 下午2:01:55
	 * @param vo
	 * @return
	 */
	@Override
	public QueryPrefixPostfixAndConds parse(Object vo) {
		
		Conds conds = new Conds();
		QueryPrefixPostfixAndConds returnVal = new QueryPrefixPostfixAndConds(conds);
		Class<? extends Object> voClazz = vo.getClass();
		
		if (voClazz.isAnnotationPresent(QueryPrefixAndPostfix.class)) {
			QueryPrefixAndPostfix queryPrefixAndPostfix = voClazz.getAnnotation(QueryPrefixAndPostfix.class);
			returnVal.setQueryPrefix(queryPrefixAndPostfix.queryPrefix());
			returnVal.setQueryPostfix(queryPrefixAndPostfix.queryPostfix());
			String defaultTableName = queryPrefixAndPostfix.defaultTableName();
			
			Field[] declaredFields = voClazz.getDeclaredFields();

			for (Field field : declaredFields) {
				if (field.isAnnotationPresent(QueryCond.class)) {
					QueryCond queryCond = field.getAnnotation(QueryCond.class);
					String tableName = queryCond.tableName();
					if (tableName.isEmpty()) {
						tableName = defaultTableName;
					}
					String fieldName = field.getName();
					Object fieldValue = null;

					if (!field.isAccessible()) {
						field.setAccessible(true);
					}
					try {
						fieldValue = field.get(vo);
					} catch (IllegalArgumentException e) {
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						e.printStackTrace();
					}
					
					//如果没有对属性设置值，那么说明不希望此属性作为条件
					if (fieldValue == null) {
						continue;
					}
					
					if (fieldValue instanceof String) {
						if (((String) fieldValue).trim().isEmpty()) {
							continue;
						}
					}
					
					String tableColumnName = queryCond.tableColumnName();
					String sqlOperator = queryCond.sqlOperator();
					Object param = fieldValue;
					
					if (tableColumnName.isEmpty()) {
						tableColumnName = fieldName;
					}
					
					if (sqlOperator.matches("\\s*%\\s*?like\\s*?%\\s*")) {
						sqlOperator = "like";
						param = "%" + fieldValue + "%";
					} else if (sqlOperator.matches("\\s*like\\s*?%\\s*|\\s*like\\s*")) {
						sqlOperator = "like";
						param = fieldValue + "%";
					} else if (sqlOperator.matches("\\s*%\\s*?like\\s*")) {
						sqlOperator = "like";
						param = "%" + fieldValue;
					}
					conds.addFilter(tableName, tableColumnName, sqlOperator, param);
				}
			}
			
			return returnVal;
		} else {
			throw new RuntimeException("the input parameter need to use QueryPrefix annotation in type level");
		}
	}
}
